import sys
import re
from PySpyCore import (
    get_hwnd_from_pos,
    get_title_pp,
    title_fuzzy_search,
    find_window,
)
from MoveWindow import (
    move_window_to_left_top,
    move_window_to_right_top,
    move_window_to_left_bottom,
    move_window_to_right_bottom,
    move_window,
)
import ctypes

# 使程序正常使用 MoveWindow.py
ctypes.windll.shcore.SetProcessDpiAwareness(1)


if len(sys.argv) == 3 and sys.argv[1][0:2] == "窗口" and sys.argv[2][0:3] == "移动至":
    arg1 = sys.argv[1][3:]
    arg2 = sys.argv[2][4:]

    hwnd = None
    title = None
    if re.match("^\d+[,，]\d+$", arg1):
        if len(arg1.split(",")) == 2:
            p = arg1.split(",")
        elif len(arg1.split("，")) == 2:
            p = arg1.split("，")
        hwnd = get_hwnd_from_pos(x=int(p[0]), y=int(p[1]))
        hwnd = get_title_pp(hwnd)[0]
    else:
        title = arg1
        titles = title_fuzzy_search(title)
        hwnd = find_window(titles[0])

    if arg2 == "左上角":
        move_window_to_left_top(hwnd)
    elif arg2 == "右上角":
        move_window_to_right_top(hwnd)
    elif arg2 == "左下角":
        move_window_to_left_bottom(hwnd)
    elif arg2 == "右下角":
        move_window_to_right_bottom(hwnd)
    elif re.match("^\d+[,，]\d+$", arg2):
        if len(arg2.split(",")) == 2:
            p2 = arg2.split(",")
        elif len(arg2.split("，")) == 2:
            p2 = arg2.split("，")
        x = int(p2[0])
        y = int(p2[1])
        move_window(hwnd, x, y)
